﻿
using LNet;
using OestsCommon.Model;
using System;
using System.Net.Sockets;
using Utils;
using Utils.signal.impl;

/// <summary>
/// 用来跟客户端做通信 
/// </summary>
namespace OestsServer.Net
{
    public class UnityClientChannel : ClientChannel
    {

        /// <summary>
        /// 客户端类型，基值对应项目id，每个项目的任务信息，房间信息等不一样
        /// </summary>
        public ProjectType ProjectType { get; set; }

        public UnityClientChannel() { }

        public UnityClientChannel(string listenerName, Socket socket, ClientManager mng) : base(listenerName, socket, mng)
        {
        }

        #region 定义信号 发送消息 处理函数

        /// <summary>
        /// 向指定ID 发送数据
        /// </summary>
        public static Signal<int, Response> Signal_ID = new Signal<int, Response>();


        /// <summary>
        /// 向同类客户端发送数据
        /// </summary>
        public static Signal<ProjectType, Response> Signal_Project = new Signal<ProjectType, Response>();

        /// <summary>
        /// 向同类项目 同一个房间发送数据
        /// </summary>
        public static Signal<ProjectType, int, Response> Signal_Project_Room = new Signal<ProjectType, int, Response>();

        /// <summary>
        /// 向同类客户端 同一个房间中除了特定ID 发送数据
        /// </summary>
        public static Signal<ProjectType, int, int, Response> Signal_Project_Room_Except = new Signal<ProjectType, int, int, Response>();

        /// <summary>
        /// 添加信号事件
        /// </summary>
        public override void SignalAddListener()
        {
            base.SignalAddListener();
            Signal_ID.AddListener(SendToID);
            Signal_Project.AddListener(SameProjectHandler);
            Signal_Project_Room.AddListener(SameProjectAndRoomHandler);
            Signal_Project_Room_Except.AddListener(SameProjectAndRoomExceptHandler);
        }

        /// <summary>
        /// 移除信号事件
        /// </summary>
        public override void SignalRemoveListener()
        {
            base.SignalRemoveListener();
            Signal_ID.RemoveListener(SendToID);
            Signal_Project.RemoveListener(SameProjectHandler);
            Signal_Project_Room.RemoveListener(SameProjectAndRoomHandler);
            Signal_Project_Room_Except.RemoveListener(SameProjectAndRoomExceptHandler);
        }

        private void SendToID(int stdId, Response packet)
        {
            if (Student != null && Student.DBid == stdId)
            {
                Send(packet);
            }
        }

        private void SameProjectHandler(ProjectType client, Response response)
        {
            if (this.ProjectType == client)
            {
                if (Student == null) return;
                Send(response);
            }
        }

        public void SameProjectAndRoomHandler(ProjectType client, int roomId, Response response)
        {
            if (this.ProjectType != client) return;
            if (Student == null) return;
            if (Student._room == null) return;
            if (Student._room.RoomId == roomId)
            {
                Send(response);
            }
        }

        public void SameProjectAndRoomExceptHandler(ProjectType client, int roomId, int exceptId, Response response)
        {
            if (this.ProjectType != client) return;
            if (Student == null) return;
            if (Student.DBid == exceptId) return;

            if (Student._room == null) return;

            if (Student._room.RoomId == roomId)
            {
                Send(response);
            }
        }
        #endregion

        public override bool CheckUserIsExist(/*UserType uType,*/ int userDbId)
        {
            return Manager.CheckUserIsExist(ProjectType, userDbId);
        }

        protected override void Recevie(Request request)
        {
            ServerController.Instance.UnityServerListener.Parse(this, request);
        }
        public override void ExceptionHandler(Exception e)
        {
            LogNetDebug.LogError("仿真系统处理数据异常:" + e.Message);
        }
    }

}
